package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_coupon;

import cn.com.api.service.ApiU_couponService;

import cn.com.util.*;


public class U_couponBaseController {

	private static Logger logger = Logger.getLogger(U_couponBaseController.class);
	
	@Autowired
	private ApiU_couponService apiU_couponService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_coupon-list")
	public ModelAndView u_coupon_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_coupon_list.jsp");
		
		List<ApiU_coupon> u_coupons = apiU_couponService.findU_coupons();
		mv.addObject("u_coupons", u_coupons);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_coupon-add-pre")
	public ModelAndView u_coupon_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_coupon_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_coupon u_coupon = apiU_couponService.findU_couponById(Integer.valueOf(id));
			mv.addObject("u_coupon",u_coupon);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_coupon-add")
	public void u_coupon_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String coupon_id = request.getParameter("coupon_id");
		String coupon_cate_id = request.getParameter("coupon_cate_id");
		String corp_name = request.getParameter("corp_name");
		String corp_adress = request.getParameter("corp_adress");
		String range = request.getParameter("range");
		String corp_mobile = request.getParameter("corp_mobile");
		String business_hours = request.getParameter("business_hours");
		String coupon_type = request.getParameter("coupon_type");
		String description = request.getParameter("description");
		String corp_img = request.getParameter("corp_img");
		String corp_longitude = request.getParameter("corp_longitude");
		String corp_latitude = request.getParameter("corp_latitude");
		String deposit_amount = request.getParameter("deposit_amount");
		String deposit_status = request.getParameter("deposit_status");
		String sort = request.getParameter("sort");
		String coupon_amout = request.getParameter("coupon_amout");
		String use_limit1 = request.getParameter("use_limit1");
		String coupon_count = request.getParameter("coupon_count");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		String create_time = request.getParameter("create_time");
		String user_id = request.getParameter("user_id");
		String is_active = request.getParameter("is_active");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String coupon_score = request.getParameter("coupon_score");
		String coupon_prefix = request.getParameter("coupon_prefix");
		String use_limit2 = request.getParameter("use_limit2");
		String use_limit3 = request.getParameter("use_limit3");
		String use_limit4 = request.getParameter("use_limit4");
		String coupon_img = request.getParameter("coupon_img");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(coupon_id==null || (coupon_id.trim()).equals("")){
			ApiU_coupon u_coupon = new ApiU_coupon();
            /*
		    u_coupon.setCoupon_id(coupon_id);
		    u_coupon.setCoupon_cate_id(coupon_cate_id);
		    u_coupon.setCorp_name(corp_name);
		    u_coupon.setCorp_adress(corp_adress);
		    u_coupon.setRange(range);
		    u_coupon.setCorp_mobile(corp_mobile);
		    u_coupon.setBusiness_hours(business_hours);
		    u_coupon.setCoupon_type(coupon_type);
		    u_coupon.setDescription(description);
		    u_coupon.setCorp_img(corp_img);
		    u_coupon.setCorp_longitude(corp_longitude);
		    u_coupon.setCorp_latitude(corp_latitude);
		    u_coupon.setDeposit_amount(deposit_amount);
		    u_coupon.setDeposit_status(deposit_status);
		    u_coupon.setSort(sort);
		    u_coupon.setCoupon_amout(coupon_amout);
		    u_coupon.setUse_limit1(use_limit1);
		    u_coupon.setCoupon_count(coupon_count);
		    u_coupon.setStart_time(start_time);
		    u_coupon.setEnd_time(end_time);
		    u_coupon.setExt1(ext1);
		    u_coupon.setExt2(ext2);
		    u_coupon.setExt3(ext3);
		    u_coupon.setExt4(ext4);
		    u_coupon.setExt5(ext5);
		    u_coupon.setCreate_time(create_time);
		    u_coupon.setUser_id(user_id);
		    u_coupon.setIs_active(is_active);
		    u_coupon.setProvince(province);
		    u_coupon.setCity(city);
		    u_coupon.setArea(area);
		    u_coupon.setCoupon_score(coupon_score);
		    u_coupon.setCoupon_prefix(coupon_prefix);
		    u_coupon.setUse_limit2(use_limit2);
		    u_coupon.setUse_limit3(use_limit3);
		    u_coupon.setUse_limit4(use_limit4);
		    u_coupon.setCoupon_img(coupon_img);
	        */
			apiU_couponService.insertU_coupon(u_coupon);
		}
		//edit
		else{
			ApiU_coupon u_coupon = apiU_couponService.findU_couponById(Integer.valueOf(coupon_id));
	        /*
		    u_coupon.setCoupon_id(coupon_id);
		    u_coupon.setCoupon_cate_id(coupon_cate_id);
		    u_coupon.setCorp_name(corp_name);
		    u_coupon.setCorp_adress(corp_adress);
		    u_coupon.setRange(range);
		    u_coupon.setCorp_mobile(corp_mobile);
		    u_coupon.setBusiness_hours(business_hours);
		    u_coupon.setCoupon_type(coupon_type);
		    u_coupon.setDescription(description);
		    u_coupon.setCorp_img(corp_img);
		    u_coupon.setCorp_longitude(corp_longitude);
		    u_coupon.setCorp_latitude(corp_latitude);
		    u_coupon.setDeposit_amount(deposit_amount);
		    u_coupon.setDeposit_status(deposit_status);
		    u_coupon.setSort(sort);
		    u_coupon.setCoupon_amout(coupon_amout);
		    u_coupon.setUse_limit1(use_limit1);
		    u_coupon.setCoupon_count(coupon_count);
		    u_coupon.setStart_time(start_time);
		    u_coupon.setEnd_time(end_time);
		    u_coupon.setExt1(ext1);
		    u_coupon.setExt2(ext2);
		    u_coupon.setExt3(ext3);
		    u_coupon.setExt4(ext4);
		    u_coupon.setExt5(ext5);
		    u_coupon.setCreate_time(create_time);
		    u_coupon.setUser_id(user_id);
		    u_coupon.setIs_active(is_active);
		    u_coupon.setProvince(province);
		    u_coupon.setCity(city);
		    u_coupon.setArea(area);
		    u_coupon.setCoupon_score(coupon_score);
		    u_coupon.setCoupon_prefix(coupon_prefix);
		    u_coupon.setUse_limit2(use_limit2);
		    u_coupon.setUse_limit3(use_limit3);
		    u_coupon.setUse_limit4(use_limit4);
		    u_coupon.setCoupon_img(coupon_img);
			*/
			apiU_couponService.updateU_coupon(u_coupon);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_coupon-delete")
	public void u_coupon_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_couponService.deleteU_coupon(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



